﻿using System;
using Company.BLL;
using Company.BLL.KDT.GC;
using Company.BLL.DuLieuChuan;
using Company.Interface.GC;
using System.Data;


namespace Company.Interface.KDT.GC.PCTFormTemplate
{
    public partial class FormDieuChinhMaSP : BaseForm
    {
        public Company.BLL.KDT.GC.PhuKienDangKy pkdk = new Company.BLL.KDT.GC.PhuKienDangKy();
        public Company.BLL.KDT.GC.PhuKien pk = new Company.BLL.KDT.GC.PhuKien();
        public Company.BLL.KDT.GC.HopDong hopdong = new Company.BLL.KDT.GC.HopDong();        
        public bool isAdd = true;
        private string dvt = string.Empty;
        private int STTHang = 0;
        public FormDieuChinhMaSP()
        {
            InitializeComponent();
        }

        private void txtMaNPL_Leave(object sender, EventArgs e)
        {
            
        }
       

        private void txtMaNPL_ButtonClick(object sender, EventArgs e)
        {
            SanPham sp = new SanPham();
            sp.HopDong_ID = hopdong.ID;
            SanPhamGCEditForm f = new SanPhamGCEditForm();
            f.isBrower = true;
            f.SPDetail.HopDong_ID = hopdong.ID;
            f.hongdong = hopdong;
            f.ShowDialog();
            if (f.SPDetail != null && f.SPDetail.Ma != "")
            {
                txtMaNPL.Text = f.SPDetail.Ma.Trim();
                txtTenNPL.Text = f.SPDetail.Ten.Trim();
                txtMaHSNPL.Text = f.SPDetail.MaHS;
                txtSoLuong.Text = f.SPDetail.SoLuongDangKy.ToString();
                cbDonViTinh.SelectedValue = (f.SPDetail.DVT_ID);
                cbNhomSP.SelectedValue =Company.BLL.KDT.GC.NhomSanPham.Load(f.SPDetail.NhomSanPham_ID).MaSanPham;               
            }
            
        }
        private bool CheckNPLExit(string maNPL)
        {
            foreach(PhuKien pk1 in pkdk.PKCollection)
            {
                if (pk1.ThongTinCu == maNPL.Trim())
                {
                    return true;
                }
            }
            return false; ;
        }
        

        private void txtMaSP_Leave(object sender, EventArgs e)
        {
           
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            cvError.Validate();
            if (!cvError.IsValid) return;
                if (isAdd)
                {
                    if (CheckNPLExit(txtMaNPL.Text))
                    {
                        ShowMessage("Đã điều chỉnh nguyên phụ liệu này rồi", false);
                        return;
                    }
                    pk.MaHang = txtMaSPMoi.Text.Trim();
                    pk.TenHang = txtTenNPL.Text.Trim();
                    pk.SoLuong =Convert.ToDecimal(txtSoLuong.Text);
                    pk.MaHS = txtMaHSNPL.Text.Trim();
                    pk.DVT_ID = cbDonViTinh.SelectedValue.ToString();
                    pk.ThongTinCu = txtMaNPL.Text.Trim();
                    pk.NhomSP = cbNhomSP.SelectedValue.ToString();
                    pk.STTHang = STTHang + 1;
                    pkdk.PKCollection.Add(pk);

                }
                else
                {
                    pkdk.PKCollection.Remove(pk);
                    foreach(PhuKien p in pkdk.PKCollection)
                        if(p.MaHang==txtMaNPL.Text)
                        {
                            ShowMessage("Đã điều chỉnh nguyên phụ liệu này rồi", false);
                            pkdk.PKCollection.Add(pk);
                            return;
                        }
                    pk.MaHang = txtMaSPMoi.Text.Trim();
                    pk.TenHang = txtTenNPL.Text.Trim();
                    pk.SoLuong = Convert.ToDecimal(txtSoLuong.Text);
                    pk.MaHS = txtMaHSNPL.Text.Trim();
                    pk.DVT_ID = cbDonViTinh.SelectedValue.ToString();
                    pk.ThongTinCu = txtMaNPL.Text.Trim();
                    pk.NhomSP = cbNhomSP.SelectedValue.ToString();                    
                    pkdk.PKCollection.Add(pk);
                }
                this.Close();
            }       

        private void btnClose_Click(object sender, EventArgs e)
        {
            
        }

        private void DieuChinhSoLuongNPLForm_Load(object sender, EventArgs e)
        {
            this._DonViTinh = DonViTinh.SelectAll();
            cbDonViTinh.DataSource = this._DonViTinh;
            cbDonViTinh.DisplayMember = "Ten";
            cbDonViTinh.ValueMember = "ID";
            DataTable loaiSp = Company.BLL.KDT.GC.NhomSanPham.SelectBy_HopDong_ID(hopdong.ID).Tables[0];
            cbNhomSP.DataSource = loaiSp;
            cbNhomSP.DisplayMember = "TenSanPham";
            cbNhomSP.ValueMember = "MaSanPham";
            if (!isAdd)
            {
                txtMaNPL.Text = pk.MaHang.Trim();
                txtTenNPL.Text = pk.TenHang.Trim();
                txtMaHSNPL.Text = pk.MaHS.Trim();
                txtSoLuong.Text = pk.SoLuong.ToString();                               
                cbDonViTinh.SelectedValue = pk.DVT_ID;
                cbNhomSP.SelectedValue = pk.NhomSP;
            }
            foreach (PhuKien p in pkdk.PKCollection)
            {
                if (p.STTHang > STTHang)
                    STTHang = p.STTHang;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }
    }
}